<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Speedy extends Controller{
	
	function __construct(){
		parent::Controller();
		$this->current_user = models\Current_user::get_user();
		if(!models\Current_user::get_user()) 
			show_error(array('Halaman ini tidak bisa diakses oleh anda!',anchor('','Kembali ke halaman utama')),403);
	}
	
	function index(){
		$this->load->library('pagination');

		$total_rows = $this->doctrine->em
			->createQuery('SELECT COUNT(s.id) FROM models\Speedy s')
			->getSingleScalarResult();
		
		$config = array(
			'base_url' =>  base_url().'/'.$this->uri->segment(1).'/index',
			'total_rows' => $total_rows,
			'per_page' => 10,
			'num_links' => 10,
			'full_tag_open' => "<div id='pagination'>",
			'full_tag_close' => "</div>",
		);

		$this->pagination->initialize($config);

		if(!$this->uri->segment(3)) $offset = 0;
		else $offset = $this->uri->segment(3);
		
		$speedy = $this->doctrine->em
			->createQuery('SELECT s FROM models\Speedy s ORDER BY s.id ASC')
			->setMaxResults($config['per_page'])
			->setFirstResult($offset)
			->getResult();

		foreach($speedy as $s){

			$block = new Block();

			$grid = $block->add_grid();
			$grid->add_nama_info($s,'images/orang.png');
			if(models\Current_user::previlege('Administrator'))
				$grid->add_info('Agen: '.anchor('agen/detail/'.$s->get_agen()->get_username(),$s->get_agen()->get_nama()));
			else $grid->add_info('Agen: '.$s->get_agen()->get_nama());
			$grid->add_info('Paket: '.$s->get_paket_speedy());

			$block->add_grid()
				->add_info( $s->get_alamat(),'images/rumah.png');

			$block->add_grid()
				->add_info($s->get_telepon(),'images/telepon.png');

			$block->set_class('daftar_akun');

			$this->main_content->add_block($block);
		}

		$this->main_content->add_pagination();

		$this->sidebar->create_custom('p','Seluruh langganan Speedy');
		$this->headbar->add_header();
		$this->headbar->add_menu('register','Daftar Speedy Baru',$this->uri->segment(1).'/register');
		
        $this->load->view('template', array('title' => 'Speedy'));
	}
	
	function detail($id = null){
		
		if(!$id) show_404();

		$speedy = $this->doctrine->em->getRepository('models\Speedy')->findOneBy(array('id' => $id));
		if(!$speedy) show_404();

		$this->config->load('dropdown_options');
		$option_paket = $this->config->item('paket_speedy');

		$this->main_content->add_custom('p','Detail speedy '.$speedy->get_nama());

		$block = $this->main_content->add_block('Akun')
			->add_hidden_field('id',$speedy->get_id())
			->add_editable_data('nama','Nama',$speedy->get_nama())
			->add_editable_dropdown_data('paket_speedy','Paket Speedy',$option_paket,$speedy->get_paket_speedy());

		if(models\Current_user::previlege('Administrator'))
			$block->add_editable_agen($speedy->get_agen());
		else $block->add_data('Agen',$speedy->get_agen()->get_nama());

		$this->main_content->add_block('Alamat')
			->add_editable_data('alamat','Alamat',$speedy->get_alamat());

		$this->main_content->add_block('Kontak')
			->add_editable_data('telepon','No Telepon',$speedy->get_telepon());

		$this->main_content->set_editable();
		$this->main_content->set_deletable();

		$this->headbar->add_header();
		$this->sidebar->create_identitas($speedy);

        $this->load->view('template', array('title' => 'Detail Speedy'));

    }
	
	function ajax_edit($username = null){
		if(IS_AJAX){

			$this->load->library('form_validation');

			if($this->form_validation->run('speedy') == TRUE){

				$speedy = $this->doctrine->em
					->getRepository('models\Speedy')
					->findOneBy(array('id' => $username));

				if(!$speedy) {
					$returned_data = array('status' => 'gagal');
					echo json_encode($returned_data);
					return false;
				}

				if($this->input->post('agen')){
					$agen = $this->doctrine->em
						->getRepository('models\Agen')
						->findOneBy(array('id' => $this->input->post('agen')));
					$speedy->set_agen($agen);
				}

				$speedy->set_nama($this->input->post('nama'));
				$speedy->set_alamat($this->input->post('alamat'));
				$speedy->set_telepon($this->input->post('telepon'));
				$speedy->set_paket_speedy($this->input->post('paket_speedy'));

				$this->doctrine->em->flush();

				$returned_data = array(
					'status' => 'sukses',
					'username' => $username,
				);

			} else {
				$returned_data = array(
					'status' => 'gagal',
					'error' => validation_errors(),
				);
			}

			echo json_encode($returned_data);

		} else show_404();
	}
	
	function ajax_hapus(){
		
		if(IS_AJAX){
		
			$id = $this->input->post('username');

			$speedy = $this->doctrine->em
				->getRepository('models\Speedy')
				->findOneBy(array('id' => $id));

			$this->doctrine->em->remove($speedy);
			$this->doctrine->em->flush();

			$this->session->set_flashdata('status', 'Berhasil menghapus speedy '.$id);

		} else show_404();
    }
	
	function register(){
	
		$option_agen = models\Agen::get_dropdown_options();

		$this->config->load('dropdown_options');
		$option_paket = $this->config->item('paket_speedy');
		
		$block = $this->main_content->add_block('Akun')
			->add_text_field('nama','Nama')
			->add_dropdown_field('paket_speedy','Paket',$option_paket);

		if(models\Current_user::previlege('Administrator')){
			$block->add_dropdown_field('agen','Agen',$option_agen); 
		}

		$this->main_content->add_block('Alamat')
			->add_text_field('alamat','Alamat');

		$this->main_content->add_block('Kontak')
			->add_text_field('telepon','Telepon');

		$this->main_content->set_form_register();

		$this->headbar->add_header();
		$this->sidebar->create_custom('p','Pendaftaran Speedy tidak menghasilkan akun yang dapat login ke situs ini');

		$this->load->view('template', array('title' => 'Register Speedy'));
    }

	function ajax_register(){
		if(IS_AJAX){

			$this->load->library('form_validation');

			if($this->form_validation->run('speedy') == TRUE){

				$speedy = new models\Speedy();

				if(models\Current_user::previlege('Agen'))
					$agen = $this->current_user;
				else $agen = $this->doctrine->em->find('models\Agen',$this->input->post('agen'));

				$speedy->set_agen($agen);

				$speedy->set_nama($this->input->post('nama'));
				$speedy->set_alamat($this->input->post('alamat'));
				$speedy->set_telepon($this->input->post('telepon'));
				$speedy->set_paket_speedy($this->input->post('paket_speedy'));

				//insert ke database
				$this->doctrine->em->persist($speedy);
				$this->doctrine->em->flush();

				$returned_data = array(
					'status' => 'sukses',
				);

				$this->session->set_flashdata('status', 'Berhasil manambah speedy '.$this->input->post('nama').'!');
			} else {

				$returned_data = array(
					'status' => 'gagal',
					'error' => validation_errors(),
				);
			}

			echo json_encode($returned_data);

		} else show_404(); 
	}
}
